package com.li.service.impl;

//import com.li.config.RandomCode;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.mapper.CourseMapper;
import com.li.pojo.dto.CourseDTO;
import com.li.pojo.po.Course;
import com.li.pojo.po.PageResult;
import com.li.pojo.vo.CourseVO;
import com.li.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-04-11
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * todo: 创建人修改人写死了
     *
     * @param courseDTO
     */
    @Override
    public void addCourse(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO, course);
        course.setCreateTime(LocalDateTime.now().toString());
        course.setUpdateTime(LocalDateTime.now().toString());
        course.setUpdateBy("admin");
        course.setCreateBy("admin");
//        course.setId(null);
        course.setCode(makeRandomPassword(8));
        courseMapper.insert(course);

    }

    @Override
    public PageResult getList(CourseVO courseVO) {
        Page<Course> pageBean = PageHelper.startPage(courseVO.getPageNum(), courseVO.getPageSize());
        courseMapper.getList(courseVO);
        PageResult pageResult = new PageResult();
        pageResult.setCode(200);
        pageResult.setParams(null);
        pageResult.setMsg("查询成功");
        pageResult.setRows(pageBean.getResult());
        pageResult.setTotal(pageBean.getTotal());
        return pageResult;
    }

    @Override
    public void updateCourse(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO, course);
        course.setUpdateTime(LocalDateTime.now().toString());
        courseMapper.updateCourse(course);
    }

    @Override
    public List<Course> getListbySubject(String subject) {
        return courseMapper.getListbySubject(subject);
    }

    //生成八位编码
    public String makeRandomPassword(int len) {
        char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
        StringBuilder sb ;
        Random r = new Random();
        while (true ) {
            sb = new StringBuilder();
            for (int x = 0; x < len; ++x) {
                sb.append(chars[r.nextInt(chars.length)]);
            }
            List<Course> courses = courseMapper.selectList(null);
            if (CollectionUtils.isEmpty(courses)) {
                break;
            }
            int count = 0;
            for (Course cours : courses) {
                if(sb.toString().equals(cours.toString())){
                    count++;
                }
            }
            if(count == 0){
                break;
            }
        }
        return sb.toString();
    }
}
